﻿using System;
using System.Web;

namespace Landscape
{
    class WidgetTemplate : Template
    {
        public WidgetTemplate(string xmlTemplatePath) : base(xmlTemplatePath)
        {
            OutputMethod = OutputMethods.Xml;
            Renderer.CreateXslOutput(TemplateDom, OutputMethods.Xml, null, null, "utf-8", true, false, true, true);

            // create compiled transform object
            TemplateCompiledTransform = Context.Library.compiler.Compile(TemplateDom);
        }

        public static WidgetTemplate GetTemplate(string templatePath)
        {
            if (Context.enableCaching)
            {
                if (HttpRuntime.Cache[templatePath] != null)
                {
                    WidgetTemplate cached = (WidgetTemplate)HttpRuntime.Cache[templatePath];

                    return cached;
                }
            }

            WidgetTemplate template = new WidgetTemplate(templatePath);

            // saves object into cache
            if (Context.enableCaching)
            {
                HttpRuntime.Cache.Insert(templatePath, template, new System.Web.Caching.CacheDependency(template.DependenciesPaths.ToArray()));
            }

            return template;
        }
    }
}
